public class Solution5 {
    //322. 零钱兑换
    public static int coinChange(int[] coins, int amount) {
        int n = coins.length;

        int[][] dp  = new int[n+1][amount+1]; //dp[i][j]：从前i个数中选，恰好等于j的硬币的最小个数
        for (int i = 1; i < amount+1; i++) {
            dp[0][i] = -1;
        }

        for (int i = 1; i < n+1; i++) {
            for (int j = 1; j < amount+1; j++) {
                dp[i][j] = dp[i-1][j];
                if (j >= coins[i-1] && dp[i][j-coins[i-1]] != -1) {
                    if (dp[i][j] == -1) {
                        dp[i][j] = dp[i][j-coins[i-1]] + 1;
                    } else {
                        dp[i][j] = Math.min(dp[i][j],dp[i][j-coins[i-1]] + 1);
                    }

                }
            }
        }
        return dp[n][amount];
    }
    public static void main(String[] args) {
        int[] coins = {1,2,5};
        int amount = 11;
        int ret = coinChange(coins,amount);
        System.out.println(ret);
    }
}
